package com.example.naozhong;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends Activity implements TimePicker.OnTimeChangedListener {
    public static final String ACTION_ALARM="zhong";
    PendingIntent sender;
    private TimePicker tptime;
    private int hourOfDay=0;
    private int minute=0;
    private AlarmManager am=null;
    private Calendar calendar=Calendar.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tptime=(TimePicker)super.findViewById(R.id.time);
        tptime.setIs24HourView(true);
        tptime.setOnTimeChangedListener((TimePicker.OnTimeChangedListener) this);
        Intent i =new Intent(MainActivity.this,AlarmReceiver.class);
        i.setAction(ACTION_ALARM);
        sender=PendingIntent.getBroadcast(
                MainActivity.this,0,i,PendingIntent.FLAG_UPDATE_CURRENT
        );
        hourOfDay=tptime.getCurrentHour();
        minute=tptime.getCurrentMinute();

    }
    public void clickSet(View view){
        am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender);
        Toast.makeText(MainActivity.this,"yfdgysdugri",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTimeChanged(TimePicker timePicker, int h, int m) {
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(calendar.HOUR_OF_DAY,h);
        calendar.set(Calendar.MINUTE,m);
        calendar.set(calendar.SECOND,0);
        calendar.set(calendar.MILLISECOND,0);
        hourOfDay=h;
        minute=m;

    }
}
